package com.jdjw.modules.abuttingjoint.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.abuttingjoint.entity.DwdTkrymdMzJiande;
import com.jdjw.modules.abuttingjoint.mapper.DwdTkrymdMzJiandeMapper;
import com.jdjw.modules.abuttingjoint.service.DwdTkrymdMzJiandeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.abuttingjoint.timing.TimedTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 特困人员名单-民政 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-07-01
 */
@Service
@Slf4j
@Component
@DS("sjwjcjdcz")
public class DwdTkrymdMzJiandeServiceImpl extends ServiceImpl<DwdTkrymdMzJiandeMapper, DwdTkrymdMzJiande> implements DwdTkrymdMzJiandeService {

    private static DwdTkrymdMzJiandeMapper dwdTkrymdMzJiandeMapper;

    @Resource
    public void setDwdTkrymdMzJiandeMapper(DwdTkrymdMzJiandeMapper dwdTkrymdMzJiandeMapper) {
        DwdTkrymdMzJiandeServiceImpl.dwdTkrymdMzJiandeMapper = dwdTkrymdMzJiandeMapper;
    }

    @Override
    public ResponseResult doPostTestTwoc(String time) {
        ResponseResult results = new ResponseResult();
        results.setMsg("测试关闭");
        return results;
    }

    public static void doPostTestTwoc(String time, String dateDq) {
        ArrayList list = new ArrayList();
        String url = "http://39.170.77.212:8096/sysn/data/tkrymdMz";
        ResponseResult results = new ResponseResult();

        try{
            //查询 特困人员名单-民政人员
            QueryWrapper entityWrapper = new QueryWrapper();
            entityWrapper.like("uploadtime", time);
            List<DwdTkrymdMzJiande> all = dwdTkrymdMzJiandeMapper.selectList(entityWrapper);

            if(all == null || all.size()<1){
                log.info("特困人员:数据库查询数据为0条");
                return;
            }

            for (DwdTkrymdMzJiande a:all){
                //村社行政区划代码 == null   不传
                if(a.getCsxzqhdm()!=null){
                    //将数据的上传时间改为推送时间
                    a.setUploadtime(dateDq);
                    //转化为json字符串
                    String jsonStr = JSONObject.toJSONString(a);
                    //转化为json对象
                    list.add(JSONObject.parseObject(jsonStr));
                }

            }
            if(list.size()>0){
                //发送(post)
                results = TimedTask.getHttpResponseJson(url,list);
            }else {
                log.info("特困人员：数据不全，符合条件的数据为0条");
                return;
            }

            if(results.getCode().equals(200)){
                log.info("特困人员名单-民政人员推送成功",results);
                UpdateWrapper<DwdTkrymdMzJiande> wrapper = new UpdateWrapper<>();
                wrapper.like("uploadtime",time).set("bx","1");
                int b = dwdTkrymdMzJiandeMapper.update(null,wrapper);
            }else {
                log.info("特困人员名单-民政人员推送失败",results);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }


}
